package demo3;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-03-22
 * Time: 14:53
 */
class Person implements Cloneable{
    public String name;
    public int age;
    int[] arr = {1 , 2, 3 , 4, 5 ,6 } ;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone()
            throws CloneNotSupportedException {
        //return super.clone();
        Person tmp = (Person) super.clone();
        tmp.arr = this.arr.clone();
        return tmp;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    public static void main(String[] args)
            throws CloneNotSupportedException {
        Person person1 = new Person("zhangsan",18);
        Person person2 = (Person) person1.clone();
        System.out.println("改之前");
        System.out.println(Arrays.toString(person1.arr));
        System.out.println(Arrays.toString(person2.arr));
        person2.arr[0] = 9;
        System.out.println("该之后");
        System.out.println(Arrays.toString(person1.arr));
        System.out.println(Arrays.toString(person2.arr));
    }
}
